package net.stratusprogramming.school.Game.Battle;

import java.util.ArrayList;

import net.stratusprogramming.school.Game.Battle.GUI.Background;
import net.stratusprogramming.school.Game.Battle.GUI.HUD;
import net.stratusprogramming.school.Game.Framework.Screen;
import net.stratusprogramming.school.Game.Framework.Battle.Combatant;
import net.stratusprogramming.school.Game.Framework.GUI.Fonts;
import net.stratusprogramming.school.Game.Framework.GUI.TextButton;
import net.stratusprogramming.school.Game.Framework.Scene.Camera;
import net.stratusprogramming.school.Game.Framework.Scene.Group;
import net.stratusprogramming.school.Game.Framework.System.Config;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Screens.LoserScreen;
import net.stratusprogramming.school.Game.Screens.PauseScreen;
import net.stratusprogramming.school.Game.Screens.WinnerScreen;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

/**
 * The battle screen
 * 
 * @author Dominic Vitello, Patrick Lienau
 * @since 5/7/12
 * @version 0.1
 */
public class Battle extends Screen {

	/** Input from InstanceMgr */
	private Input input;

	/** The combatants in */
	private ArrayList<Combatant> combatants;	

	/** 2D scenegraph root */
	private Group root;	

	/** Used for drawing hud */
	private HUD hud;
	
	
	/** 
	 * Starts a battle with an enemy
	 * 
	 * @param enemy enemy to battle
	 */
	public Battle(Combatant enemy) {
		// set battle to self in InstanceMgr
		InstanceMgr.battle = this;
		input = InstanceMgr.input;

		// get players
		combatants = new ArrayList<Combatant>();
		combatants.add(InstanceMgr.player);
		combatants.add(enemy);

		// place players
		InstanceMgr.player.setX(0);
		InstanceMgr.player.setY(Config.WINDOW_HEIGHT-381);
		enemy.setX(Config.WINDOW_WIDTH-256);
		enemy.setY(Config.WINDOW_HEIGHT-381);

		// create root, add players to it
		root = new Group(new Camera(false));
		root.add(new Background());

		hud = new HUD();		
		root.add(InstanceMgr.player);
		root.add(enemy);
		root.add(hud);
	}


	@Override
	public void update(int delta) {
		root.update(delta);

		if(input.isKeyPressed(Input.KEY_ESCAPE)) {
			InstanceMgr.main.setScreen(InstanceMgr.world);
		}

		if(input.isKeyPressed(Input.KEY_1)) {
			Combatant c = combatants.get(0);
			c.setHealth(c.getHealth()-10);
		}

		if(input.isKeyPressed(Input.KEY_2)) {
			Combatant c = combatants.get(0);
			c.setMana(c.getMana()-10);
		}

		/* pop win screen ? */
		if(combatants.get(1).getHealth() <= 0)	{
			InstanceMgr.main.setScreen(new WinnerScreen());
		}
		/* pop death screen ? */
		if(combatants.get(0).getHealth() <= 0)	{
			InstanceMgr.main.setScreen(new LoserScreen());
		}
	}

	@Override
	public void render(Graphics g)  {
		root.render(g);	
		
		
	}	

	public void save() {
		InstanceMgr.map.save(InstanceMgr.saveMgr);
		root.save(InstanceMgr.saveMgr);

	}

	/**
	 * @return the combatants
	 */
	public ArrayList<Combatant> getCombatants() {
		return combatants;
	}


	/**
	 * Loads things
	 */
	public void load() {
		InstanceMgr.map.load(InstanceMgr.saveMgr);
		root.load(InstanceMgr.saveMgr);
	}

}
